package cn.wtg.kotlinlib.baseUi

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import cn.wtg.kotlinlib.baseViewModel.BaseLifecycleVM

/**
 * author: wtg
 * time: 2022/11/28
 * desc:
 */
abstract class BaseMvvmActivity<out B : ViewDataBinding, VM : BaseLifecycleVM> :
    BaseBindingActivity<B>() {
    private var viewModelId = 0
    private var viewModel: VM? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = getInitViewModel()
        viewModelId = getInitViewModelId()
        binding?.setVariable(viewModelId, viewModel)
        lifecycle.addObserver(viewModel!!) //监听生命周期
    }

    /**
     * 获取viewModel
     *
     * @return viewModel
     */
    protected abstract fun getInitViewModel(): VM

    /**
     * 获取BR的id
     *
     * @return id
     */
    protected abstract fun getInitViewModelId(): Int

    override fun onDestroy() {
        super.onDestroy()
        viewModel = null
    }
}